package TheGame.Levels;

import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;

import TheGame.Entity.Mob.Mob;
import TheGame.Graphics.Screen;
import TheGame.Graphics.Sprite;
import TheGame.Levels.tiles.Tile;

public class Level {
	
	protected int width, height;
	protected int[] tiles;
	protected BufferedImage image;
	private int[] badX = {20};
	private int[] badY = {4, 8, 12};
	protected Screen screen;
	protected String path;
	
	static final String[] paths = {"/levelOne.png", "/levelTwo.png", "/levelThree.png", "/level.png"};
	
	public static int grass = 0xff00ff00;
	public static int flower = 0xffffff00;
	public static int rock = 0xff7f7f00;
	public static int wall = 0xff404040;
	public static int flower2 = 0xffffffff;
	public static int side_wall = 0xffA0A0A0;
	public static int fire = 0xffff0000;
	
	public Level(int w, int h){
		width = w;
		height = h;
		tiles = new int[width * height]; 
		generateLevel();
	}

	public Level(String path, Screen s){
		screen = s;
		this.path = path;
		loadLevel(path);
	}
	
	protected void generateLevel(){
		
	}
	
	protected void loadLevel(String path){
		try{
			image = ImageIO.read(SpawnLevel.class.getResource(path));
			width = image.getWidth();
			height = image.getHeight();
			tiles = new int[width * height];
			image.getRGB(0, 0, width, height, tiles, 0, width);
		} catch (IOException e){
			e.printStackTrace();
			System.out.println("Excpetion! Could not load level file!");
		}
		if (path.equals("/level.png")){
			badX[0] = 20;
			badY[0] = 4;
			badY[1] = 8;
			badY[2] = 12;
		}
		else if (path.equals(paths[0])){
			badX[0] = 11;
			badY[0] = 16;
			badY[1] = -1;
			badY[2] = -1;
 		}
	}
	
	public void update(){
	}
	
	@SuppressWarnings("unused")
	private void time(){
		
	}
	
	public void render(int xScroll, int yScroll, Screen screen){
		screen.setOffset(xScroll, yScroll);
		int x0 = xScroll >> 4;
		int x1 = (xScroll + screen.width + 16) >> 4;
		int y0 = yScroll >> 4;
		int y1 = (yScroll + screen.height + 16) >> 4;
		for (int y = y0; y < y1; y++){
			for (int x = x0; x < x1; x++){
				//System.out.println(x + " " + y);
				//System.out.println(getTile(x, y));
				getTile(x, y).render(x, y, screen);
			}
		}
		if (path.equals(paths[3])){
			if (Mob.deer.getHealth() >= 0) screen.renderMob(4 * 16, 8 * 16, Sprite.deer);
		}
	}
	
	public Tile getTile(int x, int y){
		if (x < 0 || x >= width || y < 0 || y >= height) return Tile.voidTile;
		if (tiles[x + y * width] == grass) return Tile.grass;
		if (tiles[x + y * width] == flower) return Tile.flower;
		if (tiles[x + y * width] == rock) return Tile.rock;
		if (tiles[x + y * width] == wall) return Tile.wall;
		if (tiles[x + y * width] == side_wall) return Tile.side_wall;
		if (tiles[x + y * width] == flower2) return Tile.flower2;
		if (tiles[x + y * width] == fire) return Tile.fire;
		return Tile.voidTile;
	}
	
	public void changePath(String path){
		loadLevel(path);
	}
	
	public void changePath(int i){
		loadLevel(paths[i]);
		path = paths[i];
	}
	
	public void setTrue(int i){
		//This method must be overridden in the SpawnLevel class
	}
	
	public int[] getBadX(){
		return badX;
	}
	
	public int[] getBadY(){
		return badY;
	}

	public void displayPanel() {
		
	}
	
	public Mob[] getMobs(){
		if (path.equals(paths[3]))
			return new Mob[]{Mob.deer};
		else
			return null;
	}
}
